package org.xxpay.manage.config.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.xxpay.core.common.annotation.MethodLog;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.domain.BizResponse;
import org.xxpay.core.common.domain.XxPayPageRes;
import org.xxpay.core.common.domain.XxPayResponse;
import org.xxpay.core.entity.PayAlizftMchInfo;
import org.xxpay.core.entity.PayInterfaceTypeTemplate;
import org.xxpay.manage.common.ctrl.BaseController;
import org.xxpay.manage.common.service.RpcCommonService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author: pangxioayu
 * @date: 20/10/21
 * @description: 支付宝支付通
 */
@RestController
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/config/pay_alizft_mchInfo")
public class PayAlizftMchInfoController extends BaseController {

    @Autowired
    RpcCommonService rpcCommonService;

    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        PayAlizftMchInfo payAlizftMchInfo = getObject(param, PayAlizftMchInfo.class);
        Long count = commonService.payAlizftMchInfoService.count(payAlizftMchInfo);
        if(count == 0) return ResponseEntity.ok(XxPayPageRes.buildSuccess());
        List<PayAlizftMchInfo> infoList = commonService.payAlizftMchInfoService.select((getPageIndex(param) -1) * getPageSize(param), getPageSize(param), payAlizftMchInfo);
        return ResponseEntity.ok(XxPayPageRes.buildSuccess(infoList, count.intValue()));
    }

    @RequestMapping("/get")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long subMchId = getLong(param, "subMchId");
        PayAlizftMchInfo payAlizftMchInfo = commonService.payAlizftMchInfoService.findById(subMchId);
        return ResponseEntity.ok(XxPayResponse.buildSuccess(payAlizftMchInfo));
    }

    @RequestMapping("/apply")
    @ResponseBody
    public ResponseEntity<?> apply(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        PayAlizftMchInfo payAlizftMchInfo = getObject(param, PayAlizftMchInfo.class);
        if ("06".equals(payAlizftMchInfo.getMerchantType())) {
            if (StringUtils.isEmpty(payAlizftMchInfo.getAlipayLogonId()) ||
            StringUtils.isEmpty(payAlizftMchInfo.getBindingAlipayLogonId())) {
                return ResponseEntity.ok(XxPayResponse.build(RetEnum.RET_MGR_ALIPAY_ZFT_LOGO_ID_NOT_NULL));
            }
        }
        PayInterfaceTypeTemplate template = commonService.payInterfaceTypeTemplateService.findById(payAlizftMchInfo.getPassageTemplateId());
        if (template == null) return ResponseEntity.ok(XxPayResponse.build(RetEnum.RET_MGR_TEMPLATE_NOT_FOUND));
        String applymentSubmit = rpcCommonService.rpcXxPayAlizftpayApiService.alipayApplymentSubmit(payAlizftMchInfo);
        return ResponseEntity.ok(BizResponse.build(applymentSubmit));
    }

    @RequestMapping("/query")
    @ResponseBody
    public ResponseEntity<?> query(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long subMchId = getLong(param, "subMchId");
        String applymentSubmit = rpcCommonService.rpcXxPayAlizftpayApiService.queryAndUpdateStatus(subMchId);
        return ResponseEntity.ok(BizResponse.build(applymentSubmit));
    }


    /**
     * 修改商户状态
     * @return
     */
    @RequestMapping("/updateStatus")
    @ResponseBody
    @MethodLog( remark = "修改二级商户状态" )
    public ResponseEntity<?> updateStatus(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long subMchId = getLongRequired(param, "subMchId");
        Byte status = getByte(param, "status");
        PayAlizftMchInfo alizftMchInfo = commonService.payAlizftMchInfoService.findById(subMchId);
        if(alizftMchInfo == null) {
            return ResponseEntity.ok(XxPayResponse.build(RetEnum.RET_COMM_RECORD_NOT_EXIST));
        }
        PayAlizftMchInfo mchInfo = new PayAlizftMchInfo();
        mchInfo.setSubMchId(subMchId);
        mchInfo.setStatus(status);
        int count = commonService.payAlizftMchInfoService.update(mchInfo);
        if(count < 1) return ResponseEntity.ok(XxPayResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
        return ResponseEntity.ok(BizResponse.buildSuccess());
    }

    @RequestMapping("/risk_update")
    @ResponseBody
    @MethodLog( remark = "修改二级商户风控" )
    public ResponseEntity<?> updateRisk(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        param.replace("dayAmountLimit", StringUtils.deleteWhitespace(param.getString("dayAmountLimit")));
        param.replace("maxEveryAmountLimit",StringUtils.deleteWhitespace(param.getString("maxEveryAmountLimit")));

        Long dayAmountLimit = getLongRequired(param, "dayAmountLimit");
        Long maxEveryAmountLimit = getLongRequired(param, "maxEveryAmountLimit");
        PayAlizftMchInfo payAlizftMchInfo = getObject(param, PayAlizftMchInfo.class);

        // 元转分
        if (dayAmountLimit != -1) payAlizftMchInfo.setDayAmountLimit(dayAmountLimit * 100);
        if (maxEveryAmountLimit != -1) payAlizftMchInfo.setMaxEveryAmountLimit(maxEveryAmountLimit * 100);
        int count = commonService.payAlizftMchInfoService.update(payAlizftMchInfo);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    /**
     * 修改商户状态
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    @MethodLog( remark = "删除二级商户" )
    public ResponseEntity<?> delete(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long subMchId = getLongRequired(param, "subMchId");
        int count = commonService.payAlizftMchInfoService.delete(subMchId);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }


    /**
     * 二级商户统计
     * @return
     */
    @RequestMapping("/count")
    @ResponseBody
    public ResponseEntity<?> count(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        String smid = getString(param, "smid");
        Integer page = getInteger(param, "page");
        Integer limit = getInteger(param, "limit");
        Long passageTemplateId = getLongRequired(param, "passageTemplateId");
        // 订单起止时间
        String createTimeStartStr = getString(param, "createTimeStart");
        String createTimeEndStr = getString(param, "createTimeEnd");
        Long count = commonService.payAlizftMchInfoService.subDataCount(smid, passageTemplateId, createTimeStartStr, createTimeEndStr);
        if(count == 0) return ResponseEntity.ok(XxPayPageRes.buildSuccess());
        List<Map> mapList = commonService.payAlizftMchInfoService.subData((getPageIndex(page) -1) * getPageSize(limit), getPageSize(limit), passageTemplateId, smid, createTimeStartStr, createTimeEndStr);
        return ResponseEntity.ok(XxPayPageRes.buildSuccess(mapList, count.intValue()));
    }

}
